package com.worldvision.main.util;

import java.io.IOException;
import java.util.Random;

import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

//import lib.System.SYS;
//import lib.System.Utility.Report;
//
//import com.sarees.main.util.WebUtil;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleniumException;


public class SystemUtil
{
	//static WebUtil w ;//= new WebUtil();;
	static Selenium  selenium;
	public SystemUtil()
	{
		selenium  = (new WebDriverBackedSelenium((new FirefoxDriver()),"http:\\www.worldvisionindia.com"));
		//this.w = w;
	}
	public static boolean isWebTablePresent(String locator)
	{
		//WebUtil x = new WebUtil();
		
		if(selenium.isElementPresent(locator))
		{
			return true;
		}
		return false;
	}
	public static int getWebTableRows(String locator)
	{
		return 0;
	}
	public static int getWebTableColumns(String locator)
	{
		return 0;
	}
	public static boolean isCellWithTextPresent(String locator,String text)
	{
		return false;
	}
	public static String getCelltext(String locator,int col,int row)
	{
		return null;
	}
	public static void clickOnCell(String locator,int col,int row)
	{
	
	}
	public static void clickObjectInCell(String tableLocator,String objLocator)
	{
		
	}
	public static boolean isCellWithTextPresentInColumn(String locator,String text,String header)
	{
		int j=0;
		int columnCount = getWebTableColumnCount(locator);
		String xpath;
		for(int i=0;i<=columnCount-1;i++)
		{
			if(selenium.getTable(locator+".0."+i).equalsIgnoreCase(header))
			{
				j = i;
				break;
			}
		}
		int rowCount=getWebTableRowCount(locator);
		
		for(int i=0;i<=rowCount-1;i++)
		{
			xpath = "xpath=//table[@id='"+locator+"']//tr["+(i+1)+"]/td["+(j+1)+"]/Input";
			if(selenium.getTable(locator+"."+i+"."+j).equalsIgnoreCase(text))
			{
				return true;
			}
			else if(selenium.isElementPresent(xpath))
			{
				if(selenium.getValue(xpath).equalsIgnoreCase(text))
				return true;
			}
		}
		return false;
	}
	public static int getWebTableColumnCount(String locator) {
		// TODO Auto-generated method stub
		int value=0;
		if(isWebTablePresent(locator))
		{
//			try
//			{
				for(int i=0;;i++)
				{
					try
					{
						value = i;
						String temp=selenium.getTable(locator+"."+0+"."+i);
						
					}
					catch(SeleniumException e)
					{
						
						//throw (e);
						return value;
					}
				}
//			}
//			catch(SeleniumException e)
//			{
//				value=i-1;
//				
//			}
		}
		return value;
	
	}
	public static int getWebTableRowCount(String locator) {
		// TODO Auto-generated method stub
		int value=0;
		if(isWebTablePresent(locator))
		{
//			try
//			{
				for(int i=0;;i++)
				{
					try
					{
						value = i;
						String temp=selenium.getTable(locator+"."+i+".0");
					
					}
					catch(SeleniumException e)
					{
						//value = i-1;
						//throw (e);
						return value;
					}
				}
//			}
//			catch(SeleniumException e)
//			{
//				value=i-1;
//				
//			}
		}
		return value;
	}
	public static int getColumnNumberHdrTxt(String locator, String header) {
		// TODO Auto-generated method stub
		int j=0;
		int columnCount = getWebTableColumnCount(locator);
		for(int i=0;i<=columnCount-1;i++)
		{
			if(selenium.getTable(locator+".0."+i).equalsIgnoreCase(header))
			{
				return i;
			}
		}
		return 0;
	}
	public static int getColumnNumberHdrId(String locator, String header) {
		// TODO Auto-generated method stub
		int j=0;
		int columnCount = getWebTableColumnCount(locator);
		for(int i=0;i<=columnCount-1;i++)
		{
			if(selenium.getAttribute("xpath=//table[@id='"+locator+"']//tr["+1+"]/th["+(i+1)+"]@id").equalsIgnoreCase(header))
			{
				return i;
			}
		}
		return 0;
	}
	public static int getRowNumberOfText(String locator,
			String header, String text) {
		int j=0;
		int columnCount = getWebTableColumnCount(locator);
		for(int i=0;i<=columnCount-1;i++)
		{
			if(selenium.getTable(locator+".0."+i).equalsIgnoreCase(header))
			{
				j = i;
				break;
			}
		}
		int rowCount=getWebTableRowCount(locator);
		for(int i=0;i<=rowCount-1;i++)
		{
			if(selenium.getTable(locator+"."+i+"."+j).equalsIgnoreCase(text))
			{
				return i;
			}
		}
		
		// TODO Auto-generated method stub
		return 0;
	}
	
	public static String getXPathofObject(String tableLocator,int row,String columnHeader,String objectLocator)
	{
		//get the row number
		
		//get column number
		int columnNumber = getColumnNumberHdrTxt(tableLocator,columnHeader);
		
		//create xpath
		String xpath = tableLocator+"."+row+"."+columnNumber;
		//test the object's present
		if(selenium.isElementPresent(xpath))
		{
			return xpath;
			
		}
		xpath = "//table[@id='"+tableLocator+"']/tbody/tr["+row+"]/td["+columnNumber+"]/"+objectLocator;
		if(selenium.isElementPresent(xpath))
		{
			return xpath;
		}
		return null;
		
		
		
	}
	public static String getLocatorOfCell(String tableLocator, int rowNum,
			int columnNumber) {
		// TODO Auto-generated method stub
		//create xpath
				String xpath = tableLocator+"."+rowNum+"."+columnNumber;
				//test the object's present
				if(selenium.isElementPresent(xpath))
				{
					return xpath;
					
				}
				xpath = "xpath=//table[@id='"+tableLocator+"']/tbody/tr["+(rowNum+1)+"]/td["+(columnNumber+1)+"]/input";
				if(selenium.isElementPresent(xpath))
				{
					return xpath;
				}
				return null;
	}
	public static boolean isCellWithTextPresent(String tableLocator,
			String value, String columnHeader, String item) 
	{
		// TODO Auto-generated method stub
		int itemColumn=0;
		int column=0;
		int columnCount = getWebTableColumnCount(tableLocator);
		int row = 0;
		String xpath;
		for(int i=0;i<=columnCount-1;i++)
		{
			if(selenium.getTable(tableLocator+".0."+i).equalsIgnoreCase(columnHeader))
			{
				column = i;
				
			}
			//else if(selenium.getTable(tableLocator+".0."+i).equalsIgnoreCase(DATA.ITEMHEADER))
				//	{
					//	itemColumn = i;
					//}
			
		}
		int rowCount=getWebTableRowCount(tableLocator);
		
		for(int i=0;i<=rowCount-1;i++)
		{
			//xpath = "xpath=//table[@id='"+tableLocator+"']//tr["+(i+1)+"]/td["+(j+1)+"]/Input";
			if(selenium.getTable(tableLocator+"."+i+"."+itemColumn).equalsIgnoreCase(item))
			{
				row = i;
				break;
			}
			
		}
		if(selenium.getTable(tableLocator+"."+row+"."+column).equalsIgnoreCase(item))
		{
			return true;
			
		}
		else 
		{
			xpath = "xpath=//table[@id='"+tableLocator+"']//tr["+(row+1)+"]/td["+(column+1)+"]/";
			
			if(selenium.isElementPresent(xpath))
			{
				if(selenium.getText(xpath).equalsIgnoreCase(value))
				{
					return true;
				}
				else
				{
					xpath = "xpath=//table[@id='"+tableLocator+"']//tr["+(row+1)+"]/td["+(column+1)+"]/Input";
					if(selenium.isElementPresent(xpath))
					{
						if(selenium.getText(xpath).equalsIgnoreCase(value))
						{
							return true;
						}
						
					}}
			}
		}
		
		
		return false;
	}
}
